/*
 * @Author: your name
 * @Date: 2021-04-14 10:25:18
 * @LastEditTime: 2021-04-19 14:34:32
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /flutter_demo/lib/pages/tabs/Home.dart
 */
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:device_info/device_info.dart';
import 'package:connectivity/connectivity.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:package_info/package_info.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String _data = '';
  var subscription;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Map userInfo = {'name': 'koa', 'age': 12};
    var a = json.encode(userInfo); // 转换为json字符串

    String info = '{"name": "koa", "age": 12}';
    var b = json.decode(info); // json字符串转换为map
    // print(b is Map);
    // DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    // AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    // IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    // print('Running on ${iosInfo.utsname.machine}');
    // SharedPreferences sd = await SharedPreferences.getInstance();
    // print(sd.getString('name'));

    subscription = Connectivity()
        .onConnectivityChanged
        .listen((ConnectivityResult result) {
      if (result == ConnectivityResult.wifi) {
        print('wifi');
      } else if (result == ConnectivityResult.mobile) {
        print('mobile');
      } else {
        print('no network');
      }
    });
  }

  @override
  dispose() {
    super.dispose();

    subscription.cancel();
  }

  getData() async {
    var apiUrl = Uri.parse(
        'http://rap2api.taobao.org/app/mock/276191/example/1611191337482');
    var result = await http.get(apiUrl);
    if (result.statusCode == 200) {
      print(json.decode(result.body));
      // _data = json.decode(result.body);
      setState(() {
        _data = json.decode(result.body)['msg'];
      });
    } else {
      print(result.statusCode);
    }
  }

  // postData() async {
  //   var apiUrl = Uri.parse(
  //       'http://rap2api.taobao.org/app/mock/276191/example/1611191337482');
  //   var result = await http.post(apiUrl, body: {
  //     'name': 'koa',
  //     'age': 10
  //   });
  //   if (result.statusCode == 200) {
  //     print(json.decode(result.body));
  //   } else {
  //     print(result.statusCode);
  //   }
  // }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
        child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        ElevatedButton(
          onPressed: () {
            Navigator.pushNamed(context, '/search', arguments: {'id': '12#'});
          },
          // onPressed: () {
          //   Navigator.of(context)
          //       .push(MaterialPageRoute(builder: (context) => SearchPage()));
          // },
          child: Text('跳转到搜索页'),
        ),
        ElevatedButton(
          onPressed: () {
            // Navigator.push(
            //     context, MaterialPageRoute(builder: (context) => FormsPage()));
            Navigator.pushNamed(context, '/form');
          },
          child: Text('跳转到表单并传值'),
        ),
        ElevatedButton(
          onPressed: () {
            // Navigator.push(context,
            //     MaterialPageRoute(builder: (context) => ProductPage()));
            Navigator.pushNamed(context, '/product');
          },
          child: Text('跳转到商品'),
        ),
        ElevatedButton(
          onPressed: () {
            // Navigator.push(
            //     context, MaterialPageRoute(builder: (context) => AppBarDemo()));
            Navigator.pushNamed(context, '/appBarDemo');
          },
          child: Text('跳转到appBarDemo'),
        ),
        ElevatedButton(
          onPressed: () {
            // Navigator.push(context,
            //     MaterialPageRoute(builder: (context) => TabControlPage()));
            Navigator.pushNamed(context, '/tabControl');
          },
          child: Text('tabControl'),
        ),
        ElevatedButton(
          onPressed: getData,
          child: Text('请求数据'),
        ),
        ElevatedButton(
          onPressed: () {
            Navigator.pushNamed(context, '/renderData');
          },
          child: Text('跳转到接口渲染数据页'),
        ),
        ElevatedButton(
          onPressed: () {
            Navigator.pushNamed(context, '/refreshData');
          },
          child: Text('跳转到上拉下拉加载数据'),
        ),
        ElevatedButton(
          onPressed: () {
            Navigator.pushNamed(context, '/takePhoto');
          },
          child: Text('跳转到拍照上传'),
        ),
        ElevatedButton(
          onPressed: () {
            Navigator.pushNamed(context, '/video');
          },
          child: Text('跳转播放视频'),
        ),
        ElevatedButton(
          onPressed: () {
            Navigator.pushNamed(context, '/storage');
          },
          child: Text('跳转本地存储'),
        ),
        ElevatedButton(
          onPressed: () {
            Navigator.pushNamed(context, '/scanQrcode');
          },
          child: Text('跳转扫码'),
        )
      ],
    ));
  }
}
